all files / src/validators/ bid.validator.ts

96.67% Statements 58/60
95.65% Branches 44/46
100% Functions 3/3
98.25% Lines 56/57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112     15×     14×       14×     13×     11× 11× 11×                         15×                   13×     12×     11×     10×                     26×   26×         26×   26×    
import { Game, PlayersBid, Phase } from '../game.interfaces';
import { Bid, SHARE_STOCK, IncreaseBid } from '../game.actions';
import { getNextTurn } from '../helpers/players.helpers';
import { hasMarriage } from '../helpers/cards.helpers';
import * as _ from 'lodash';
import { hasPlayerAlreadyPassed, isMaxBid, hasTwoPasses, getHighestBid, isAchievableBid, isValidBidValue, getBidWinner } from '../helpers/bid.helpers';
import { isTableEmpty, getTotalWonCards } from '../helpers/battle.helpers';
 
export function canBid(state: Game, action: Bid): boolean {
    if (state.phase !== Phase.BIDDING_IN_PROGRESS) {
        return false;
    }
 
    Iif(isBiddingFinished(state)) {
        return false
    }
 
    if (!isAchievableBid(action)) {
        return false;
    }
 
    if (!isValidBidValue(action)) {
        return false;
    }
 
    const lastBiddingPlayerId = state.bid[0].player;
    const nextAllowedPlayerToBid = getNextTurn(state.players, lastBiddingPlayerId);
    if (action.player !== nextAllowedPlayerToBid) {
        return false;
    }
 
    if (action.pass) {
        if(hasPlayerAlreadyPassed(state.bid, action.player)) {
            return false;
        } else {
            return true;
        }
    }
 
    const lastBidValue = getHighestBid(state.bid);
    if (action.bid <= lastBidValue.bid) {
        return false;
    }
 
    const playerId = action.player;
    const playerCards = state.cards[playerId];
    const hasPlayerMarriage = hasMarriage(playerCards);
 
    if (action.bid >= 130 && !hasPlayerMarriage) {
        return false;
    }
    return true;
};
 
export function canIncreaseBid(state: Game, action: IncreaseBid): boolean {
    if (!_.includes([
        Phase.BIDDING_FINISHED,
        Phase.SHARE_STOCK,
        Phase.ASSIGN_STOCK,
        Phase.BATTLE_START,
        Phase.TRICK_START,
        Phase.TRICK_IN_PROGRESS
    ], state.phase)) {
        return false;
    }
 
    if (!isAchievableBid(action)) {
        return false;
    }
 
    if (!isValidBidValue(action)) {
        return false;
    }
 
    if (action.pass) {
        return false;
    }
 
    if (getBidWinner(state.bid).player !== action.player) {
        return false;
    }
 
    const lastBidValue = getHighestBid(state.bid);
    if (action.bid <= lastBidValue.bid) {
        return false;
    }
 
    if (action.bid >= 130 && !hasMarriage(state.cards[action.player])) {
        return false;
    }
 
    if(state.phase === Phase.TRICK_IN_PROGRESS) {
        if (!isTableEmpty(state.battle)) { return false; } //some cards throw on the table
        if (getTotalWonCards(state) > 0) { return false; } //some tricks already finished
    }
 
    return true;
};
 
export function isBiddingFinished(state: Game): boolean {
    Iif (state.bid.length === 0) return false;
 
    const isMax = _.chain(state.bid)
        .head()
        .thru(isMaxBid)
        .value();
 
    const hasTwoPlayerPasses = hasTwoPasses(state.bid);
 
    return hasTwoPlayerPasses || isMax;
}